<?php

declare(strict_types=1);

namespace app\backend\controller\sys;

use app\common\controller\Basic;
use app\common\service\sys\UserGroupService;
use app\common\service\sys\UserGroupMemberService;

/**
 * 系统用户分组 控制器
 * @author kaige
 * @update  2022-07-13
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class UserGroup extends Basic
{

  // 初始化
  protected function initialize()
  {
    parent::initialize();
    // 当前控制器服务实例化
    $this->service = new UserGroupService();
  }

  /**
   * 列表
   */
  public function index()
  {
    if ($this->request->isGet()) {
      $data = input('param.');
      $data['pid'] = 0;
      return json($this->service->_list($data));
    }
    return _jerr(1000);
  }

  /**
   * 添加
   */
  public function add()
  {
    if ($this->request->isPost()) {
      $data = input('param.');
      $this->_form_filter($data);
      return json($this->service->saveForm($data));
    }
    if ($this->request->isGet()) {
      $data = [];
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 编辑
   */
  public function edit()
  {
    $id = input('id/d', 0);
    if ($id === 0) {
      return _jerr(1022);
    }
    if ($this->request->isPost()) {
      $data = input('param.');
      $this->_form_filter($data);
      return json($this->service->saveForm($data));
    }
    if ($this->request->isGet()) {
      $data = $this->service->getInfoById($id);
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 添加编辑-表单数据处理
   */
  protected function _form_filter(&$data)
  {
    url_decode($data);
    if (isset($data['id']) && isset($data['pid']) && $data['id'] == $data['pid']) { //不允许 id =pid
      $data['pid'] = 0;
    }
  }

  /**
   * 删除
   */
  public function del()
  {
    if ($this->request->isPost()) {
      $id = input('id', '');
      if ($id === 0) {
        return _jerr(1002);
      }
      return json($this->service->_del($id));
    }
    return _jerr(1000);
  }

  /*******组用户******/
  /**
   * 组用户列表
   * @return array|string
   */
  public function groupuserlist()
  {
    if ($this->request->isGet()) {
      $gus = new UserGroupMemberService();
      $data = input('param.');
      if (!isset($data['group_id']) || empty($data['group_id'])) {
        return json(_layui(0, []));
      }
      return json($gus->_list($data));
    }
    return _jerr(1000);
  }

  /**
   * 添加组用户
   * @return array|string
   */
  public function groupuseradd()
  {
    if ($this->request->isPost()) {
      $data = input('param.');
      return json((new UserGroupMemberService())->saveForm($data));
    }
    if ($this->request->isGet()) {
      return _jok('ok');
    }
    return _jerr(1000);
  }

  /**
   * 编辑组用户
   * @return array|string
   */
  public function groupuseredit()
  {
    $gus = new UserGroupMemberService();
    if ($this->request->isPost()) {
      $data = input('param.');
      return json($gus->saveForm($data));
    }
    if ($this->request->isGet()) {
      $id = input('id/d', 0);
      $data = $gus->getInfoById($id);
      return _jok('ok', $data);
    }
    return _jerr(1000);
  }

  /**
   * 删除组用户
   */
  public function groupuserdel()
  {
    if ($this->request->isPost()) {
      $id = input('id/s', '');
      if ($id === 0) {
        return _jerr(1002);
      }
      if ((new UserGroupMemberService())->_del($id)) {
        return _jok(1025);
      };
      return _jerr(1026);
    }
    return _jerr(1000);
  }
}
